import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.util.GenericOptionsParser;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HbaseTest {
    @Test
    public void getTid() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.0.83:2181");
        conf.set("zookeeper.znode.parent","/hbase-unsecure");
        Connection conn = ConnectionFactory.createConnection(conf);
        Table table = conn.getTable(TableName.valueOf("tsdb-uid"));
        Result r = table.get(new Get("1111111111".getBytes()));
        Cell cell = r.getColumnLatestCell("id".getBytes(), "tagv".getBytes());
        System.out.println(Arrays.toString(CellUtil.cloneValue(cell)));
        conn.close();
    }

    @Test
    public void getTidf() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.0.85:2181");
        Connection conn = ConnectionFactory.createConnection(conf);
        Table table = conn.getTable(TableName.valueOf("tsdb-uid"));
        Result r = table.get(new Get("60".getBytes()));
        Cell cell = r.getColumnLatestCell("id".getBytes(), "tagv".getBytes());
        System.out.println(Arrays.toString(CellUtil.cloneValue(cell)));
        conn.close();
    }

    @Test
    public void t1() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.0.85:2181");
        Connection conn = ConnectionFactory.createConnection(conf);
        String ids = "14,95";
        List<byte[]> tidList = new ArrayList<byte[]>();
        List<Get> getList = new ArrayList<Get>();
        for (String id:ids.split(",")){
            getList.add(new Get(id.getBytes()));
        }
        Table table = conn.getTable(TableName.valueOf("tsdb-uid"));
        for (Result r:table.get(getList)){
            Cell c = r.getColumnLatestCell("id".getBytes(), "tagv".getBytes());
            if(c!=null) {
                byte[] bytes = CellUtil.cloneValue(c);
                tidList.add(bytes);
                System.out.println(Arrays.toString(bytes));
            }
        }
    }

    @Test
    public void getRowKey() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.0.83:2181");
        conf.set("zookeeper.znode.parent","/hbase-unsecure");
        Connection conn = ConnectionFactory.createConnection(conf);
        Table table = conn.getTable(TableName.valueOf("tsdb"));
        Scan s = new Scan().setFilter(new PageFilter(1));
        ResultScanner rs = table.getScanner(s);
        rs.forEach(x->{
            byte[] row = x.getRow();
            System.out.println(Arrays.toString(row));
            System.out.println(row.length);
        });
        conn.close();
    }

    @Test
    public void t() throws IOException {
        String[] args = new String[]{"-Dmapreduce.output.fileoutputformat.compress=true","b"};
        String[] r = new GenericOptionsParser(HBaseConfiguration.create(), args).getRemainingArgs();
        System.out.println(Arrays.toString(r));
    }

    @Test
    public void other(){
        byte[] a = {1, 2, 3};
        byte[] b = {1, 2, 3};
        System.out.println(Arrays.toString(Arrays.copyOfRange(a,0,2)));
    }
}
